package cn.shlbysj.demo.service.impl;

import cn.shlbysj.demo.common.ConstansUtil;
import cn.shlbysj.demo.common.PageResult;
import cn.shlbysj.demo.common.Result;
import cn.shlbysj.demo.common.UuidUtil;
import cn.shlbysj.demo.entity.ArtInfo;
import cn.shlbysj.demo.entity.ArtInfoExample;
import cn.shlbysj.demo.entity.DiscusInfo;
import cn.shlbysj.demo.entity.DiscusInfoExample;
import cn.shlbysj.demo.mapper.ArtInfoMapper;
import cn.shlbysj.demo.mapper.DiscusInfoMapper;
import cn.shlbysj.demo.service.IDiscusInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service("discusInfoService")
@Transactional
public class IDiscusInfoServiceImpl implements IDiscusInfoService{
    @Autowired
    private DiscusInfoMapper discusInfoDao;

    @Override
    public PageResult findPage(String artId, int page, int rows) {
        //设置分页查询条件
        PageHelper.startPage(page,rows);
        DiscusInfoExample example=new DiscusInfoExample();
        example.setOrderByClause("`create_time` desc");
        DiscusInfoExample.Criteria criteria = example.createCriteria();
        criteria.andArtIdEqualTo(artId);
        Page<DiscusInfo> pageList = (Page<DiscusInfo>) discusInfoDao.selectByExample(example);
        return new PageResult(pageList.getTotal(),pageList.getResult());
    }

    @Override
    public Result insert(DiscusInfo discusInfo) {
        discusInfo.setdId(UuidUtil.getUUID());
        discusInfo.setCreateTime(new Date());
        discusInfo.setIsReplay(ConstansUtil.DIS_NO_REPLAY);
        int ret=discusInfoDao.insert(discusInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }

    @Override
    public Result update(DiscusInfo discusInfo) {
        int ret=discusInfoDao.updateByPrimaryKeySelective(discusInfo);
        if(ret>0){
            return new Result(ConstansUtil.RESULT_SUCCESS,ConstansUtil.RESULT_SUCCESS_MESSAGE, "","");
        }
        return new Result(ConstansUtil.RESULT_FAIL,ConstansUtil.RESULT_FAIL_MESSAGE,"","");
    }
}
